import java.util.LinkedList;
import java.util.Queue;

public class myStackUseQueue {
    //用队列实现栈
    public Queue<Integer> queue1;
    public Queue<Integer> queue2;

    public myStackUseQueue() {
        queue1=new LinkedList<>();
        queue2=new LinkedList<>();
    }
    //入栈
    public void push(int val){
        if(!queue1.isEmpty()){
            queue1.offer(val);
        }else if(!queue2.isEmpty()){
            queue2.offer(val);
        }else {
            queue1.offer(val);
        }
    }
    //出栈
    public int pop() {
        if (empty()) {
            return -1;
        }
        if (!queue1.isEmpty()) {
            int size = queue1.size();
            for (int i = 0; i < size - 1; i++) {
                queue2.offer(queue1.poll());
            }
            return queue1.poll();
        } else {
            int size = queue2.size();
            for (int i = 0; i < size - 1; i++) {
                queue1.offer(queue2.poll());
            }
            return queue2.poll();
        }
    }
        //取栈顶端元素
        public int peek () {
            int value=0;
            if (empty()) {
                return -1;
            }
            if (!queue1.isEmpty()) {
                int size = queue1.size();
                for (int i = 0; i < size ; i++) {
                    value=queue1.poll();
                    queue2.offer(value);
                }
                return value;
            } else {
                int size = queue2.size();
                for (int i = 0; i < size; i++) {
                    value=queue2.poll();
                    queue1.offer(value);
                }
                return value;
            }

        }
        //判空
      public boolean empty(){
            return queue1.isEmpty() && queue2.isEmpty();
        }
    }
